package 比较;

import java.util.HashSet;
import java.util.Set;

public class Student {
    private Integer age;
    private String name;

    public Student(Integer age, String name) {
        this.age = age;
        this.name = name;
    }

    public Student() {
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof Student) {
            Student student = (Student) obj;
            return this.age.equals(student.age) && this.name.equals(student.name);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.age.hashCode() + this.name.hashCode();
    }

    public static void main(String[] args) {
        Student p1 = new Student(1, "Alice");
        Student p2 = new Student(1, "Alice");

        System.out.println(p1.equals(p2));   // true（重写了equals）
        System.out.println(p1.hashCode() == p2.hashCode());   // false（未重写hashCode）

        Set<Student> set = new HashSet<>();
        set.add(p1);
        set.add(p2);
        System.out.println(set.size());
    }
}
